<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/4
 * Time: 11:03
 */

namespace app\alliance\controller;


use app\service\MemberService;
use app\service\TaskService;
use Redis\Redis;
use think\Db;

class BigRunner extends Common
{
    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 大转轮首页
     */
    function index()
    {
        $redis = new Redis();
        $time = strtotime(date('Y-m-d 23:59:59')) - time();
        # 查询大转轮
        $active = $redis->hGetJson('bigRunner', 'info');
        if (empty($active)) {
            # 查询数据库
            $active = Db::table('active')
                ->where("start_time <= '" . date("Y-m-d H:i:s") . "' and end_time >= '" . date("Y-m-d H:i:s") . "' and status = 1 and category = 1")
                ->find();
            if (!empty($active)) {
                if ($active['end_time'] < date('Y-m-d 23:59:59')) {
                    $time = strtotime($active['end_ime']) - time();
                }
                $redis->hSetJson('bigRunner', 'info', $active, $time);
            }
        }
        if (empty($active)) {
            return array('status' => false, 'msg' => '没有开启的大转轮活动','playerInfo'=>array(
                'head_portrait'=>$this->memberInfo['head_portrait'],
                'name'=>$this->memberInfo['name'],
                'title'=>$this->memberInfo['title'],
                'integral'=>$this->memberInfo['integral']
            ));
        }
        # 判断今天是否是第一次
        $firstInfo = $redis->hGetJson('lottery', $this->code);
        if (!empty($firstInfo)) {
            # 不是第一次
            $firstState = 0;
        } else {
            # 是第一次
            $firstState = 1;
        }
        # 查询活动奖品
        $prize = $redis->hGetJson('bigRunnerPrize', strval($active['id']));
        if (empty($prize)) {
            $prize = Db::table('active_reward')->field('*,title name')->where(array('active_id' => $active['id'], 'active_category' => 1))->select();
            # 奖品总概率
            $probability = array_sum(array_column($prize, 'reward_rate'));
            if ($probability < 1) {
                # 奖品总概率小于1 ,用谢谢参与补齐
                array_push($prize, array('name' => '谢谢参与','category'=>0, 'reward_rate' => priceFormat((1 - $probability))));
            }
            if(!empty($prize)){
                foreach ($prize as $k=>$v){
                    if($v['category']==1){
                        #积分
                        $prize[$k]['image'] = imgUrl('bigRunner/integral.png');
                    }elseif($v['category']==2){
                        # 金币
                        $prize[$k]['image'] = imgUrl('bigRunner/gold.png');
                    }elseif($v['category']==3){
                        # 卡券
                        $prize[$k]['image'] = imgUrl('bigRunner/voucher.png');
                    }elseif($v['category']==4){
                        # 恢复券
                        $prize[$k]['image'] = imgUrl('bigRunner/material.png');
                    }else{
                        # 谢谢参与
                        $prize[$k]['image'] = imgUrl('bigRunner/smile.png');
                    }
                }
            }
            $redis->hSetJson('bigRunnerPrize', strval($active['id']), $prize, $time);
        }
        # 查询奖品领取记录
        $logInfo = Db::table('active_rewardlog ar')
            ->field('ar.nickname,date_format(ar.create_time,"%Y-%m-%d %H:%i") time,reward_cate,
            (case reward_cate 
            when 1 then\'积分\'
            when 2 then \'金币\'
            when 3 then \'卡券\'
            when 4 then \'恢复券\'
            end) cate,
            reward_title,reward_num,unit,voucher_title')
            ->where(array('active_id' => $active['id'], 'active_category' => 1))
            ->order('ar.id desc')
            ->select();
        return array('status' => true, ',msg' => '查询成功', 'prize' => $prize, 'logInfo' => $logInfo, 'firstState' => $firstState,'activeId'=>$active['id'],
            'playerInfo'=>array(
                'head_portrait'=>$this->memberInfo['head_portrait'],
                'name'=>$this->memberInfo['name'],
                'title'=>$this->memberInfo['title'],
                'integral'=>$this->memberInfo['integral']
            ));
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 立即摇奖
     */
    function lotteryNow()
    {
        # 活动ID
        $activeId = input('post.activeId');
        # 推荐人ID
        $p_code = input('post.p_code');
        if (!empty($activeId)) {
            $redis = new Redis();
            # 判断今天是否是第一次
            $first = $redis->hGetJson('lottery', $this->code);
            if (!empty($first)) {
                # 判断积分是否足够
                if ($this->memberInfo['integral'] < 2) {
                    return array('status' => false, 'msg' => '积分不足');
                }
                # 扣积分
                Db::table('member_property')->where(array('member_code' => $this->code))->setDec('integral', 2);
                # 积分记录
                MemberService::addLog_integral($this->code, 2, 2, 6);
                # 更新用户信息
                $this->memberInfo['integral'] = priceFormat($this->memberInfo['integral'] - 2);
                $redis->hSetJson('memberInfo', $this->code, $this->memberInfo);
            }
            # 大转轮摇奖
            $return = MemberService::giveReward($this->memberInfo, $activeId, 1, true, true, true, $p_code);
            # 添加摇奖redis
            $time = strtotime(date('Y-m-d 23:59:59')) - time();
            $redis->hSetJson('lottery', $this->code, 'EXISTENCE', $time);
            # 完成任务=>转轮摇奖
            TaskService::finishTask($this->code, 3);
            return $return;
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 中奖记录
     */
    function logReward()
    {
        # 活动ID
        $activeId = input('post.activeId');
        if (!empty($activeId)) {
            # 查询中奖记录
            $pageMark = input('post.page');
            if (empty($pageMark)) {
                $pageMark = 0;
            }
            $listRows = 10;
            $page = $listRows * $pageMark;
            $logInfo = Db::table('active_rewardlog')
                ->field('*,date_format(create_time,"%Y-%m-%d %H:%i") time,1 account,(case reward_cate 
            when 1 then\'积分\'
            when 2 then \'金币\'
            when 3 then \'卡券\'
            when 4 then \'恢复券\'
            end) cate')
                ->where(array('active_id' => $activeId, 'active_category' => 1,'member_code'=>$this->code))
                ->order('id desc')
                ->limit($page, $listRows)
                ->select();
            return array('status' => true, 'msg' => '查询成功', 'info' => $logInfo);
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 奖项设置
     */
    function awardSetting()
    {
        # 活动ID
        $activeId = input('post.activeId');
        if (!empty($activeId)) {
            # 查询活动奖品
            $info = Db::table('active_reward')
                ->where(array('active_id' => $activeId, 'active_category' => 1))
                ->select();
            if(!empty($info)){
                foreach ($info as $k=>$v){
                    $info[$k]['typeTitle'] = getsPrizeCategory($v['category']);
                    if($v['category']==1){
                        #积分
                        $info[$k]['image'] = imgUrl('bigRunner/integral.png');
                    }elseif($v['category']==2){
                        # 金币
                        $info[$k]['image'] = imgUrl('bigRunner/gold.png');
                    }elseif($v['category']==3){
                        # 卡券
                        $info[$k]['image'] = imgUrl('bigRunner/voucher.png');
                    }elseif($v['category']==4){
                        # 恢复券
                        $info[$k]['image'] = imgUrl('bigRunner/material.png');
                    }else{
                        # 谢谢参与
                        $info[$k]['image'] = imgUrl('bigRunner/smile.png');
                    }
                }
            }
            return array('status' => true, 'msg' => '查询成功', 'info' => $info);
        } else {
            return array('status' => false, 'msg' => '参数为空');
        }
    }
}